//------------------------------------------------------------------------------
///
///	\file
///		dsp_frame.c
///
///	\brief
///		DSP framework file
///
///	\Description
///		This DSP effect file shall support numerous DSP experiments without
///		recoding the effect over and over.
///
//	$Id: dsp_vol.c 308 2014-12-04 20:30:50Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	The MIT License (MIT)
//
//	Copyright (c) 2014 A.C. Verbeck
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.

//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <stdint.h>
#include <stdbool.h>

#include <sndfile.h>

#include "dsp.h"

//------------------------------------------------------------------------------
// Local data
//------------------------------------------------------------------------------
static SF_INFO	sf;
static double	param_d[DBL_PARAM_CT];
static double	atten_dB = 0.0;

//------------------------------------------------------------------------------
///
///	\brief
///		parameter init / processing / end functions
///
///	\Description
///		These functions are called as follows:
///		dsp_param_init:		is called at the start of DSP processing
///		dsp_param_process:	is called after each audio buffer is processed
///		dsp_param_end:		is called at the end of DSP processing
///
///	\param
///		none
///
///	\return
///		none
///
//------------------------------------------------------------------------------
void dsp_param_init(double pdp[])
{
	for (int i=0; i<DBL_PARAM_CT; i++)		param_d[i] = pdp[i];				//	Copy all four parameters to a local array
	atten_dB = pow(10.0, param_d[0]/20.0);										//	Calculate attenuation
}

void dsp_param_process(void)
{
}

void dsp_param_end(void)
{
}

//------------------------------------------------------------------------------
///
///	\brief
///		dsp buffer init function
///
///	\param
///		SF_INFO* sfinfo: Pointer to a sound-file information structure
///
///	\return
///		none
///
//------------------------------------------------------------------------------
void dsp_buff_init(SF_INFO* sfinfo)
{
	int chan = sfinfo->channels;												//	save channels for local usage
	int fmt = sfinfo->format;													//	save format for function

	sf = *sfinfo;																//	save sound file info for the file

	if (chan != 1) {															//	How many channels does this effect support
		printf("This DSP effect can only process single channel files\n");
		exit(0);
	}
	if ((fmt & SF_FORMAT_SUBMASK) != SF_FORMAT_FLOAT) {							//	Data format
		printf("This DSP effect can only process 32-bit floating point files\n");
		exit(0);
	}
	if ((fmt & SF_FORMAT_ENDMASK) != SF_ENDIAN_FILE) {							//	Endian file types
		printf("This DSP effect can only process native endian files\n");
		exit(0);
	}
}

//------------------------------------------------------------------------------
///
///	\brief
///		dsp buffer processing function
///
///	\param
///		double*		b_out:	Buffer to place the modified data
///		double*		b_in:	Incoming audio data buffer
///		sf_count_t	ct:		Number of items to process in this function
///
///	\return
///		none
///
//------------------------------------------------------------------------------
void dsp_buff_process(double* b_out, double* b_in, sf_count_t ct)
{
	for (sf_count_t i=0; i<ct; i++)												//	Loop through the input buffer...
		*b_out++ = (*b_in++) * atten_dB;										//	... the output value is equal to the input value multiplied by the current attenuation
}

void dsp_buff_end(void)
{
}

//
//	End: dsp_vol.c
//